package com.lv.suyiyong.hfr.ui

import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.viewpager2.widget.ViewPager2
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SPStaticUtils
import com.blankj.utilcode.util.SizeUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lv.common.base.BaseActivity
import com.lv.common.ktx.LoadSrc
import com.lv.common.utils.ARoutUtils
import com.lv.common.utils.Constants
import com.lv.suyiyong.hfr.R
import com.lv.suyiyong.hfr.databinding.ActivityWelcomeBinding

import kotlin.math.roundToInt

@Route(path = ARoutUtils.WelComeActivityURL)
class WelComeActivity : BaseActivity<ActivityWelcomeBinding>() {
    lateinit var mAdapter: BaseQuickAdapter<Int, BaseViewHolder>
    private val mBeans = mutableListOf<Int>()


    override fun getLayoutRes() = R.layout.activity_welcome
    override fun initView() {
        super.initView()
        initStatusBar(this, true, true)
    }


    override fun initConfig() {
        super.initConfig()
        mBeans.clear()
        mBeans.add(com.lv.common.R.drawable.bg_welcome_1)
        mBeans.add(com.lv.common.R.drawable.bg_welcome_2)
        mBeans.add(com.lv.common.R.drawable.bg_welcome_3)
        addPoint()
        mAdapter = object : BaseQuickAdapter<Int, BaseViewHolder>
            (R.layout.item_welcome, mBeans) {
            override fun convert(holder: BaseViewHolder, item: Int) {
                val ivImage: ImageView = holder.getView<ImageView>(R.id.iv_image)
                ivImage.LoadSrc(item)
            }
        }
        mBinding.viewpager.adapter = mAdapter
        mBinding.tvEntry.setOnClickListener {
            SPStaticUtils.put(Constants.ISFRISTLOAD, true)
            SPStaticUtils.put(Constants.ISOPEND, true)
            ARouter.getInstance().build(ARoutUtils.HomeActivityURL).navigation()
            finish()

        }
    }

    private fun addPoint() {
        mBinding.llParent.removeAllViews()
        mBeans.forEachIndexed { index, _ ->
            val viewPoint = View(this)
            mBinding.llParent.addView(viewPoint)
            val layoutParams = viewPoint.layoutParams as LinearLayout.LayoutParams
            layoutParams.width = SizeUtils.dp2px(8.0f)
            layoutParams.height = SizeUtils.dp2px(8.0f)
            if (index < mBeans.size - 1) {
                layoutParams.marginEnd = SizeUtils.dp2px(16.0f)
            }
            viewPoint.layoutParams = layoutParams
            viewPoint.setBackgroundResource(com.lv.common.R.drawable.shape_point_nomal)
        }

    }

    private var distance = SizeUtils.dp2px(24.0f)
    override fun initEvents() {
        super.initEvents()
        mBinding.viewpager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels)
                //测出页面滚动时小红点移动的距离，并通过setLayoutParams(params)不断更新其位置
                LogUtils.e("pos==$position==offset==$positionOffset==pix==$positionOffsetPixels")
                val leftMargin = distance * (position + positionOffset)
                val layoutParams =
                    mBinding.viewPointSelected.layoutParams as ConstraintLayout.LayoutParams
                layoutParams.marginStart = leftMargin.roundToInt()
                mBinding.viewPointSelected.layoutParams = layoutParams
                setPageView(position, positionOffset, positionOffsetPixels)
            }
        })
    }

    /**
     *
     */
    private var mPosition = 0
    private fun setPageView(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
        when (mPosition) {
            0 -> {
                mBinding.tvTitle.text = "全国工厂"
                mBinding.tvSubTitle.text = "海量工厂随你挑"
            }
            1 -> {
                mBinding.tvTitle.text = "多种找货方式"
                mBinding.tvSubTitle.text = "分类简洁，一键找货"
            }
            2 -> {
                mBinding.tvTitle.text = "更多商家入驻"
                mBinding.tvSubTitle.text = "开启商业新机遇"
            }
        }
        var alpha = 0.0f
        if (mPosition >= position) {//向右滑
            if (positionOffset > 0.5) {
                mPosition = position + 1
                alpha = positionOffset
            } else {
                alpha = 1 - positionOffset
                mPosition = position
            }
            mBinding.tvTitle.alpha = alpha
            mBinding.tvSubTitle.alpha = alpha
            LogUtils.e("alpha=$alpha")


        } else {//向左滑
            alpha = positionOffset
            if (positionOffset == 0.0f) {
                alpha = 1.0f
            }
            mBinding.tvTitle.alpha = alpha
            mBinding.tvSubTitle.alpha = alpha
        }
        if (positionOffset == 0.0f) {
            mPosition = position
        }
        if (mPosition == 2) {
            mBinding.tvEntry.visibility = View.VISIBLE
            mBinding.tvEntry.alpha = alpha
        } else {
            mBinding.tvEntry.visibility = View.GONE
        }
    }

    override fun initData() {
        super.initData()

    }


}